
#include "PendingDB.h"

/** Get all the pending host key updates (key fingerprints) that need
 * checking. The resulting string listmust be deleted by the caller.
 * 
 * @return A string list of each host, new key fingerprint and old key
 * fingerprint, meaning that the number of strings in the string list
 * should be divisible by three. */
std::vector<char *> *PendingDB::getAllPendingHostKeyUpdates()
{
   return sqlScanAllText("select * from 'host_key_updates';");
}

/** Remove a pending key update from the database.
 * 
 * @param hostname The hostname whose key does not need updating any
 * more. */
void PendingDB::removePendingHostKeyUpdate(const char *hostname)
{
   sqlInstantExecf("delete from 'host_key_updates' where hostname='%s';", hostname);
}

/** Add a pending key update to the database.
 * 
 * @param hostname The host whose key needs updating.
 * 
 * @param oldkeyfingerprint The old key fingerprint (as a hexadecimal
 * string for display).
 * 
 * @param newkeyfingerprint The new key fingerprint (as a hexadecimal
 * string for display). */
void PendingDB::addNewHostKeyUpdateRequest(const char *hostname, const char *oldkeyfingerprint, const char *newkeyfingerprint)
{
   sqlInstantExecf("insert or replace into host_key_updates (hostname, oldkeyfingerprint, newkeyfingerprint) values ('%s', '%s', '%s');",
      hostname, oldkeyfingerprint, newkeyfingerprint);
}

/** Recreate any missing tables in this database. */
void PendingDB::createTablesIfNecessary()
{
   ensureTablesAreCreated(1,
      "host_key_updates", "(hostname char(256) primary key not null, oldkeyfingerprint char(64), newkeyfingerprint char(64))");
}
